package com.neusoft.restaurant.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.restaurant.config.utils.PageUtils;
import com.neusoft.restaurant.config.utils.RedisUtils;
import com.neusoft.restaurant.entity.PageResult;
import com.neusoft.restaurant.entity.ResDishes;
import com.neusoft.restaurant.mapper.ResDishesMapper;
import com.neusoft.restaurant.service.IResDishesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 菜品Service业务层处理
 * 
 * @author zhongda
 * @date 2023-10-13
 */
@Service
public class ResDishesServiceImpl implements IResDishesService
{
    @Autowired
    private ResDishesMapper resDishesMapper;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 查询菜品
     * 
     * @param id 菜品主键
     * @return 菜品
     */
    @Override
    public ResDishes selectResDishesById(Long id)
    {

        if (redisUtils.exists("dishes"+id)){
            return (ResDishes) redisUtils.get("dishes"+id);
        }else {
            ResDishes resDishes= resDishesMapper.selectResDishesById(id);
            redisUtils.set("dishes"+id,resDishes);
            return resDishes;

        }
    }

    /**
     * 查询菜品列表
     * 
     * @param resDishes 菜品
     * @return 菜品
     */
    @Override
    public List<ResDishes> selectResDishesList(ResDishes resDishes)
    {
        return resDishesMapper.selectResDishesList(resDishes);
    }

    @Override
    public PageResult selectResDishesList2(ResDishes resDishes, Integer pageNum, Integer pageSize) {

        //1、PageHelper开始分页，传入第几页和每页多少条记录两个参数
        PageHelper.startPage(pageNum, pageSize);
        //2、数据库进行查询
        List<ResDishes> resDisheses = resDishesMapper.selectResDishesList(resDishes);
        //3、对返回值进行判断
        if (resDisheses.size()==0){
            return null;
        }
        //4、对查询的结果进行封装
        PageInfo<ResDishes> resDishesPageInfo = new PageInfo<>(resDisheses);
        PageResult pageResult = PageUtils.getPageResult(resDishesPageInfo);

        return pageResult;
    }

    /**
     * 新增菜品
     * 
     * @param resDishes 菜品
     * @return 结果
     */
    @Override
    public int insertResDishes(ResDishes resDishes)
    {
        resDishes.setCreateTime(new Date());
        return resDishesMapper.insertResDishes(resDishes);
    }

    /**
     * 修改菜品
     * 
     * @param resDishes 菜品
     * @return 结果
     */
    @Override
    public int updateResDishes(ResDishes resDishes)
    {
        resDishes.setUpdateTime(new Date());
        return resDishesMapper.updateResDishes(resDishes);
    }

    /**
     * 批量删除菜品
     * 
     * @param ids 需要删除的菜品主键
     * @return 结果
     */
    @Override
    public int deleteResDishesByIds(Long[] ids)
    {
        return resDishesMapper.deleteResDishesByIds(ids);
    }

    /**
     * 删除菜品信息
     * 
     * @param id 菜品主键
     * @return 结果
     */
    @Override
    public int deleteResDishesById(Long id)
    {
        return resDishesMapper.deleteResDishesById(id);
    }
}
